package dao

import (
	"vtrip/internal/model"
)

func (d *Dao) FindAllArticle(pageOffset, pageSize int) ([]*model.ArticlePart, error) {
	article := &model.Article{}
	return article.All(d.engine, pageOffset, pageSize)
}

func (d *Dao) CreateArticle(userId int,
	title, coverUrl, introduce, content string) error {

	article := &model.Article{
		Title:     title,
		CoverUrl:  coverUrl,
		Introduce: introduce,
		Content:   content,
		AuthorId:  uint32(userId),
	}
	return article.Create(d.engine)
}

func (d *Dao) GetArticleDetail(userId, articleId int) (*model.ArticleDetail, error) {
	article := &model.Article{
		Model: &model.Model{
			ID: articleId,
		},
	}

	articleDetail, err := article.GetDetail(d.engine)
	if err != nil {
		return nil, err
	}

	articleVisit := &model.ArticleVisit{
		UserId:    userId,
		ArticleId: articleId,
	}

	err = articleVisit.Create(d.engine)
	if err != nil {
		return nil, err
	}

	return articleDetail, nil
}

func (d *Dao) LikeArticle(userId, articleId int) error {
	articleVisit := &model.ArticleVisit{
		UserId:    userId,
		ArticleId: articleId,
	}

	// 如果没有该用户的访问记录，则新增一条
	err := articleVisit.Create(d.engine)
	if err != nil {
		return err
	}

	// 增加点赞标记
	err = articleVisit.Dislike(d.engine)
	return err
}

func (d *Dao) DisLikeArticle(userId, articleId int) error {
	articleVisit := &model.ArticleVisit{
		UserId:    userId,
		ArticleId: articleId,
	}

	// 如果没有该用户的访问记录，则新增一条
	err := articleVisit.Create(d.engine)
	if err != nil {
		return err
	}

	// 把点赞标记取消掉
	err = articleVisit.Dislike(d.engine)
	return err
}
